package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.apiResult.ZhtcRe;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.apiQuery.AddOnlineMonthTicketQuery;
import com.ruoyi.system.apiService.HomeParkService;
import com.ruoyi.system.domain.ZsyyParkTicket;
import com.ruoyi.system.domain.ZsyyParkTicketType;
import com.ruoyi.system.mapper.ZsyyParkTicketMapper;
import com.ruoyi.system.mapper.ZsyyParkTicketTypeMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.bo.ZsyyParkAddBo;
import com.ruoyi.system.bo.ZsyyParkQueryBo;
import com.ruoyi.system.bo.ZsyyParkEditBo;
import com.ruoyi.system.domain.ZsyyPark;
import com.ruoyi.system.mapper.ZsyyParkMapper;
import com.ruoyi.system.vo.ZsyyParkVo;
import com.ruoyi.system.service.IZsyyParkService;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 停车场管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkServiceImpl extends ServicePlusImpl<ZsyyParkMapper, ZsyyPark> implements IZsyyParkService {
    @Autowired
    private ZsyyParkMapper iZsyyParkMapper;
    @Autowired
    private ZsyyParkTicketTypeMapper ticketTypeMapper;
    @Autowired
    private ZsyyParkTicketMapper ticketMapper;
	@Resource
	private HomeParkService homeParkService;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyParkVo queryById(Long id){
        return getVoById(id, ZsyyParkVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkVo> queryPageList(ZsyyParkQueryBo bo) {
    	bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyParkVo> result = iZsyyParkMapper.selectListByQuery(PageUtils.buildPage(), bo);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkVo> queryList(ZsyyParkQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        return iZsyyParkMapper.queryListByExport(bo);
    }

    private LambdaQueryWrapper<ZsyyPark> buildQueryWrapper(ZsyyParkQueryBo bo) {
        LambdaQueryWrapper<ZsyyPark> lqw = Wrappers.lambdaQuery();
        lqw.eq(!CommonUtil.isEmpty(bo.getCommunityId()), ZsyyPark::getCommunityId, bo.getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getParkCode()), ZsyyPark::getParkCode, bo.getParkCode());
        lqw.like(StrUtil.isNotBlank(bo.getCommunityName()), ZsyyPark::getCommunityName, bo.getCommunityName());
        lqw.like(StrUtil.isNotBlank(bo.getParkName()), ZsyyPark::getParkName, bo.getParkName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkAddBo bo) {
        ZsyyPark add = BeanUtil.toBean(bo, ZsyyPark.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkEditBo bo) {
        ZsyyPark update = BeanUtil.toBean(bo, ZsyyPark.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyPark entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public void addHeiheTicket(ZsyyParkTicket parkTicket, String parkCode) {
		try {

			ZsyyParkTicketType ticketType = ticketTypeMapper.getTicketType(parkTicket.getMonthTicketConfigId());
			Map toTicket = new HashMap();
			toTicket.put("carNo",parkTicket.getCarNo());
			toTicket.put("billCode","msdw"+ DateUtil.format(new Date(),"yyyyMMddHHmmss"));
			toTicket.put("parkCodes",parkCode);
			toTicket.put("originPrice",ticketType.getPrice());
			toTicket.put("favorPrice",parkTicket.getFavorPrice());
			toTicket.put("payMode",4);
			toTicket.put("payModeRemark","线下支付");
			toTicket.put("payOrigin",7);
			toTicket.put("payOriginRemark",parkTicket.getPayOriginRemark());
			toTicket.put("userName",parkTicket.getUserName());
			toTicket.put("userPhone", StringUtils.isEmpty(parkTicket.getUserPhone())?"13123456789":parkTicket.getUserPhone());
			toTicket.put("monthTicketConfigId",ticketType.getMonthTicketConfigId());
			List timePeriodList = new ArrayList();
			Map timePeriod = new HashMap();
			timePeriod.put("startTime", DateUtil.format(parkTicket.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
			timePeriod.put("endTime",DateUtil.format(parkTicket.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
			timePeriodList.add(timePeriod);
			toTicket.put("timePeriodList",timePeriodList);
			Map redata = null;
			String toTicketStr = JSON.toJSONString(toTicket);
			AddOnlineMonthTicketQuery query = JSON.parseObject(toTicketStr, AddOnlineMonthTicketQuery.class);
			if(parkTicket.getMqttCarId()!=null&&parkTicket.getMqttCarId()>0){
				query.setAction("edit");
				query.setID((long)(int)parkTicket.getMqttCarId());
			}else{
				query.setAction("add");
			}

			ZhtcRe re = homeParkService.addOnlineMonthTicket(query);
			redata = JSON.parseObject(JSON.toJSONString(re),Map.class);
			if("业务成功".equals(redata.get("message"))){
				Map data1 = (Map)redata.get("data");
				parkTicket.setMqttCarId(Integer.parseInt(data1.get("monthTicketId").toString()));
				ticketMapper.updateById(parkTicket);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
